package com.mvp.shq.washer.ui.home;

import com.mvp.shq.washer.entity.Banner;
import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.Washer;
import com.mvp.shq.washer.entity.result.WasherInfoResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.http.RxSchedulers;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/19.
 */

public class HomePresenter extends HomeConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void banner() {
        model.banner()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<List<Banner>>(view) {
            @Override
            protected void onSuccees(BaseResult<List<Banner>> t) throws Exception {
                if (!t.isError()){
                    view.getBannerSuccess(t.getData());
                }else {
                    view.getBannerFail(t.getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                view.getBannerFail("未知错误");
            }
        });
    }

    @Override
    public void getWasherInfo(Washer washer) {
        String token = SpUtil.getToken();
        model.getWasherInfo(token,washer.getWasherNo())
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<WasherInfoResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<WasherInfoResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getWasherInfoS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show("获取洗衣机信息失败");
                    }
                });
    }

    @Override
    public void getOrderInfo() {
        if (SpUtil.getUser() == null){
            return;
        }
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.getOrder(token,userId)
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<OrderInfo>(view) {
                    @Override
                    protected void onSuccees(BaseResult<OrderInfo> t) throws Exception {
                        if (!t.isError() && t.getData()!=null){
                               view.hasOrder(t.getData());
                        }else {
                            view.notOrder();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        view.notOrder();
                    }
                });
    }

    @Override
    public void getRestTime(String orderNo) {
        String token = SpUtil.getToken();
        model.getRestTime(token,orderNo)
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe();
    }


}
